//
//  BYTreeView.m
//  BYTreeView
//
//  Created by Bau-yin on 16/1/6.
//  Copyright (c) 2016年 Bau-yin. All rights reserved.
//

#import "BYTreeView.h"
#import "BYTreeNodeCell.h"

@interface BYTreeView ()<UITableViewDelegate, UITableViewDataSource>
@end

@implementation BYTreeView

#pragma mark - init
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style])
    {
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.nodeData nodeArray:NO].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BYTreeNodeData *nodeData = [self.nodeData nodeArray:NO][indexPath.row];
    BYTreeNodeCell *cell =  [self.treeDelegate tree:self cellForNodeData:nodeData];
    cell.nodeDelegate = self;
    cell.nodeData = nodeData;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.treeDelegate respondsToSelector:@selector(tree:heightForNodeData:)])
    {
        BYTreeNodeData *data = [self.nodeData nodeArray:NO][indexPath.row];
        CGFloat nodeHeight = [self.treeDelegate tree:self heightForNodeData:data];
        return nodeHeight;
    }
    return 0;
}

#pragma mark - BYTreeViewNodeDelegate
- (BOOL)shouldExpandAtSeletedNode:(BYTreeNodeCell *)node
{
    if ([self.treeDelegate respondsToSelector:@selector(tree:didSelectNodeCell:)])
    {
        [self.treeDelegate tree:self didSelectNodeCell:node];
    }
    
    if ([self.treeDelegate respondsToSelector:@selector(tree:shouldExpandAtSeletedNode:)])
    {
        return [self.treeDelegate tree:self shouldExpandAtSeletedNode:node];
    }
    else if(node.nodeData.child.count <= 0)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

- (void)didExpandAtSeletedNode:(BYTreeNodeCell *)node
{
    // 获取变动的行索引
    NSUInteger beginIndex = [[self.nodeData nodeArray:NO] indexOfObject:node.nodeData] + 1;
    NSUInteger indexCount = [[node.nodeData nodeArray:NO] count];
    NSMutableArray *indexArray = [NSMutableArray array];
    for (int i = 0; i < indexCount; i++)
    {
        [indexArray addObject:[NSIndexPath indexPathForRow:beginIndex + i inSection:0]];
    }
    
    // 用插入和删除函数就会产生动画
    if (node.nodeData.isExpand == NO)
    {
        [self deleteRowsAtIndexPaths:indexArray withRowAnimation:(UITableViewRowAnimationTop)];
    }
    else
    {
        [self insertRowsAtIndexPaths:indexArray withRowAnimation:(UITableViewRowAnimationBottom)];
    }
    
    if (beginIndex == 1)
    {
         [node setNeedsDisplay];
    }
   
    if ([self.treeDelegate respondsToSelector:@selector(tree:didExpandAtSeletedNode:)])
    {
        [self.treeDelegate tree:self didExpandAtSeletedNode:node];
    }
}

- (CGFloat)cellHeightAtNode:(BYTreeNodeCell *)node
{
    if ([self.treeDelegate respondsToSelector:@selector(tree:heightForNodeData:)])
    {
        CGFloat nodeHeight = [self.treeDelegate tree:self heightForNodeData:node.nodeData];
        return nodeHeight;
    }
    return 0;
}

- (void)setNodeData:(BYTreeNodeData *)nodeData
{
    _nodeData = nodeData;
    [self reloadData];
}
@end
